/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the "Foxmon" extension for Firefox.
*
* The developer of the Original Code is Saravana Kumar.
*
* Portions created by the Initial Developer are 
* Copyright (C) 2006 the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*   Saravana Kumar <saravanannkl@gmail.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
* ***** END LICENSE BLOCK ***** */

function Foxmon_Init() {
    self.setTimeout(Foxmon_UpdateStatusBar, 5000);
    var appcontent = document.getElementById("appcontent");
    if (appcontent)
        appcontent.addEventListener("DOMContentLoaded", Foxmon_HookDocument, true);
}

function Foxmon_OpenReportsTab() {
	gBrowser.selectedTab = gBrowser.addTab('chrome://foxmon/content/reports/FoxmonReports.html');
}

function Foxmon_UpdateStatusBar() {
    var now = new Date();
    var yesterday =  new Date(new Date(now.getTime() - 86400000)); 
    var prevHour =  new Date(new Date(now.getTime() - 3600000)); 
    
    var todayBTimeCond = "select round(sum(PageVisitDuration)/3600.0, 2) Time  from BrowsingHistory where Year = " + now.getFullYear() + " and Month = " + (now.getMonth() + 1) + " and Day = " + now.getDate();
    var currentHourBTimeCond = todayBTimeCond + " and hour = " + now.getHours();
    var prevHourBTimeCond = "select round(sum(PageVisitDuration)/60.0, 0) Time  from BrowsingHistory where Year = " + prevHour.getFullYear() + " and Month = " + (prevHour.getMonth() + 1) + " and Day = " + prevHour.getDate() + " and hour = " + prevHour.getHours();
    var yesterdayBTimeCond = "select round(sum(PageVisitDuration)/3600.0, 2) Time  from BrowsingHistory where Year = " + yesterday.getFullYear() + " and Month = " + (yesterday.getMonth() + 1) + " and Day = " + yesterday.getDate();
    var results = FoxmonStorageHelper.executeQuery(todayBTimeCond + " union all " + currentHourBTimeCond + " union all " + prevHourBTimeCond  + " union all " + yesterdayBTimeCond);
    
    document.getElementById('foxmon-status-bar').label = "T - " + FoxmonHelper.ConvertDecimalHoursToHHMM(results[0]) + ", CH - " + results[1] + ", PH - " + results[2] + ", Y - " + FoxmonHelper.ConvertDecimalHoursToHHMM(results[3]);
    self.setTimeout(Foxmon_UpdateStatusBar, 300000);
}

function Foxmon_HookDocument(evt) {
	var doc = evt.originalTarget;

	if (!doc || !doc.location || doc.location.hostname=="saravanankl.googlepages.com" || doc.location.protocol=="chrome:" || doc.location.protocol=="about:" || doc.location.protocol=="file:" ) return;
	var win = doc.defaultView;
	var timer = new FoxmonTimer(doc.location);

	win.addEventListener("blur", function () { timer.Stop(); }, true);
	win.addEventListener("focus", function () { timer.Start(); }, true);
						
	win.addEventListener("unload", function () { timer.CalculatePageVisitTime(); }, true);
						
	win.addEventListener("mousemove", function () { timer.ResetLastActivityTime("mousemove"); }, true);
	win.addEventListener("keypress", function () {timer.ResetLastActivityTime("keypress"); }, true);
}
 
window.addEventListener("load", function() { Foxmon_Init(); }, false);